package com.qunar.wan.service;

import com.google.common.base.Preconditions;
import com.qunar.wan.common_api.model.AdminLog;
import com.qunar.wan.common_api.model.Query;
import com.qunar.wan.common_api.model.Sort;
import com.qunar.wan.common_api.model.User;
import com.qunar.wan.common_api.model.enums.Role;
import com.qunar.wan.common_api.service.AdminLogService;
import com.qunar.wan.common_api.service.UserService;
import com.qunar.wan.common_api.service.mapper.AdminLogMapper;
import com.qunar.wan.common_api.serializer.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * User: hangyu.shen Date: 13-11-26 Time: 上午11:19
 */

@Service("adminLogService")
public class AdminLogServiceImpl extends BaseServiceImpl<AdminLog, AdminLogMapper> implements AdminLogService {
    @Resource
    private UserService userService;

    @Override
    public List<AdminLog> selectListByUserId(int userId, RowBounds rowBounds) {
        Preconditions.checkArgument(userId > 0, "(AdminLogServiceImpl)userId must greater than 0, your input is: " + userId);
        Query query = new Query().fill("userId", userId).addSort(new Sort("id", Sort.Order.DESC));
        List<AdminLog> list = selectByQuery(query, rowBounds);
        return list;
    }

    @Override
    public List<AdminLog> selectListByDate(String actionTime, int userId, RowBounds rowBounds) {
        Preconditions.checkArgument(userId > 0, "(AdminLogServiceImpl)userId must greater than 0, your input is: " + userId);
        Query query = new Query("userId", userId).fill("actionTime", actionTime).addSort(
                (new Sort("id", Sort.Order.DESC)));
        List<AdminLog> list = selectByQuery(query, rowBounds);
        return list;
    }

    @Override
    public void insertAdminLog(int userId, String action) {
        Preconditions.checkArgument(userId > 0, "(AdminLogServiceImpl)userId must greater than 0, your input is: " + userId);
        Preconditions.checkNotNull(userId);
        AdminLog adminLog = new AdminLog(userId, action, new Date());
        insertObj(adminLog);
    }

    @Override
    public int getAdminLogCountByUserId(int userId, String actionTime) {
        Preconditions.checkArgument(userId > 0, "(AdminLogServiceImpl)userId must greater than 0, your input is: " + userId);
        return getMapper().getAdminLogCount(new Query("userId", userId).fill("actionTime", actionTime));
    }

    @Override
    public String deleteOrAddAdminTran(int supAdminId, int adminId, int flag) {
        Preconditions.checkArgument(supAdminId > 0, "(AdminLogServiceImpl)supAdminId must greater than 0, your input is: " + supAdminId);
        Preconditions.checkArgument(adminId > 0, "(AdminLogServiceImpl)adminId must greater than 0, your input is: " + adminId);
        User user = userService.selectUserById(adminId);
        // flag用于判断是删除还是增加管理员,0--将管理员更改为用户，1--将用户更改为管理员
        Role role = flag == 0 ? Role.USER : Role.MANAGER;
        String msg = String.format("%s管理员'%s'成功", (flag == 0 ? "删除" : "添加"), user.getUserName());
        insertAdminLog(supAdminId, msg);
        userService.updateUserRole(adminId, role);
        return msg;
    }
}
